<?php
class PassportController extends Controller
{
    function showIndex()
    {
        // TODO: Implement index() method.
    }

    function signin()
    {
        //如果是非法登录，则直接转跳到首页
        if (!in_array($this->_post['type'], $GLOBALS['config']['user_role'])) Boot::redirect("/");
        $user = $this->modules->User;
        $user->setType($this->_post['type']);
        $user->setUserName($_POST['userName']);
        $user->setPassword($_POST['password']);
        //如果认证成功
        if ($user->auth()) {
            if ($this->isAdmin()) {
                Boot::redirect($GLOBALS['config']['user_default_dashboard'][$user->getType()]);
            }
            require_once(ROOT . "/config.inc.php");
            require_once(ROOT . "/uc_client/client.php");
            if ($user->getType() == "user") $this->putIntoPage("synCode", uc_user_synlogin($user->getMyUcMemberId()));
            $p = array("uid" => $this->getCredentials("userId"), "username" => $this->getCredentials("userName"));
            $uid = $p['uid'];
            $username = $p['username'];
            $cookietime = time() + 3600 * 24;
            $discuz_auth_key = md5(UC_KEY . $_SERVER['HTTP_USER_AGENT']);
            header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');
            $uid = intval($uid);
            $this->_setcookie($cookiepre, $cookiedomain, $cookiepath, $timestamp, $GLOBALS['_SERVER'], 'cookietime', $cookietime);
            $this->_setcookie($cookiepre, $cookiedomain, $cookiepath, $timestamp, $GLOBALS['_SERVER'], 'loginuser', $username, $cookietime);
            $this->_setcookie($cookiepre, $cookiedomain, $cookiepath, $timestamp, $GLOBALS['_SERVER'], '_auth', Util::_authcode($username, 'ENCODE', $discuz_auth_key), $cookietime);
            $this->_setcookie($cookiepre, $cookiedomain, $cookiepath, $timestamp, $GLOBALS['_SERVER'], 'isLogin', "yes");
            $this->putIntoPage("user", $this->modules->User->load($this->getCredentials("userId")));
            $this->putIntoPage("application", $this->modules->Application->loadMyCurrentVoteApplication($this->getCredentials("userId"), 1));
            $this->putIntoPage("myActivities", $this->modules->Activity->findRelatedCommentByUserId($this->getCredentials("userId")));
            $this->putIntoPage("myNews", $this->modules->News->findRelatedCommentByUserId($this->getCredentials("userId")));
            $this->render("profile");

        } else {
            Boot::redirect("/");
        }
        $this->render("front_passport");
    }

    function signout()
    {
        $_SESSION = array();
        require(ROOT . "/config.inc.php");
        require(ROOT . "/uc_client/client.php");
        $this->putIntoPage("synCode", uc_user_synlogout());
        header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');
        $this->_setcookie($cookiepre, $cookiedomain, $cookiepath, $timestamp, $GLOBALS['_SERVER'], '_auth', '', -86400 * 365);
        $this->_setcookie($cookiepre, $cookiedomain, $cookiepath, $timestamp, $GLOBALS['_SERVER'], 'sid', '', -86400 * 365);
        $this->_setcookie($cookiepre, $cookiedomain, $cookiepath, $timestamp, $GLOBALS['_SERVER'], 'isLogin', '', -86400 * 365);

        $this->putIntoPage("message", "退出成功，返回<a href='/'>首页</a>");
        //报名海选人员
        $this->putIntoPage("users", $this->modules->User->getAll());
        //每日之星
        $this->putIntoPage("dayStar", $this->modules->UserVote->dayStar(1));
        //最近的活动
        $this->putIntoPage("nearliestActivity", $this->modules->Activity->findNearliest());
        $this->putIntoPage("nearliestActivities", $this->modules->Activity->findNearlier(4));
        $this->putIntoPage("lastApplication", $this->modules->Application->findLast(14));
        $this->putIntoPage("dayStars", $this->modules->UserVote->dayStars(1, 10));
        $this->putIntoPage("bbs", $this->modules->Bbs->find(7));
        $this->putIntoPage("isLogOut", "yes");

        $this->render("front_index");
    }

    function signup()
    {
        $this->render("front_register");
    }

    function register()
    {

        if ($this->modules->User->signup($this->_post)) {
            $user = $this->modules->User;
            $user->setType("user");
            $user->setUserName($this->_post['userName']);
            $user->setPassword($this->_post['password']);
            if ($user->auth()) {

                require_once(ROOT . "/config.inc.php");
                require_once(ROOT . "/uc_client/client.php");
                if ($user->getType() == "user") $this->putIntoPage("synCode", uc_user_synlogin($user->getMyUcMemberId()));
                $p = array("uid" => $this->getCredentials("userId"), "username" => $this->getCredentials("userName"));
                $uid = $p['uid'];
                $username = $p['username'];
                $cookietime = time() + 3600 * 24;
                $discuz_auth_key = md5(UC_KEY . $_SERVER['HTTP_USER_AGENT']);
                header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');
                $uid = intval($uid);
                $this->_setcookie($cookiepre, $cookiedomain, $cookiepath, $timestamp, $GLOBALS['_SERVER'], 'cookietime', $cookietime);
                $this->_setcookie($cookiepre, $cookiedomain, $cookiepath, $timestamp, $GLOBALS['_SERVER'], 'loginuser', $username, $cookietime);
                $this->_setcookie($cookiepre, $cookiedomain, $cookiepath, $timestamp, $GLOBALS['_SERVER'], '_auth', Util::_authcode($username, 'ENCODE', $discuz_auth_key), $cookietime);
                $this->_setcookie($cookiepre, $cookiedomain, $cookiepath, $timestamp, $GLOBALS['_SERVER'], 'isLogin', "yes");
                $this->putIntoPage("user", $this->modules->User->load($this->getCredentials("userId")));
                $this->putIntoPage("application", $this->modules->Application->loadMyCurrentVoteApplication($this->getCredentials("userId"), 1));
                $this->putIntoPage("myActivities", $this->modules->Activity->findRelatedCommentByUserId($this->getCredentials("userId")));
                $this->putIntoPage("myNews", $this->modules->News->findRelatedCommentByUserId($this->getCredentials("userId")));
                $this->render("profile");
                //$this->putIntoPage("message", "注册成功，请返回首页<a href='/'>登录</a>");
            }
        }

        else {
            $this->putIntoPage("message", "注册失败，请重新<a href='" . Boot::redirect($GLOBALS['config']['user_signup_url']) . "'>注册</a>");
        }
        $this->render("front_passport");
    }

    public function test()
    {
        print_r($_SESSION);
    }

    public function password()
    {
        $step = 1;
        if (array_key_exists("userName", $this->_post) && $this->_post['userName'] != '') {
            $user = $this->modules->User->loadByUserName($this->_post['userName']);
            if ($user['userId'] > 0) {
                $_SESSION['findPasswordUserName'] = $user['userName'];
                $step = 2;
            } else {
                $this->putIntoPage("error", "对不起，系统找不到此用户名的相关信息");
            }
        }
        if (array_key_exists("email", $this->_post) && $this->_post['email'] != '' && $_SESSION['findPasswordUserName'] != '') {
            $user = $this->modules->User->loadByUserNameAndEmail($_SESSION['findPasswordUserName'], $this->_post['email']);
            if ($user['userId'] > 0) {
                $_SESSION['findPasswordEmail'] = $user['email'];
                $step = 3;
                $this->putIntoPage("question", $user['question']);
            } else {
                $this->putIntoPage("error", "对不起，您输入的邮箱有误");
            }
        }
        if (array_key_exists("answer", $this->_post) && $this->_post['answer'] != '' && $_SESSION['findPasswordUserName'] != '' && $_SESSION['findPasswordEmail'] != "") {
            $user = $this->modules->User->loadByUserNameAndEmail($_SESSION['findPasswordUserName'], $_SESSION['findPasswordEmail'], $this->_post['answer']);
            if ($user['userId'] > 0) {
                $step = 4;
                $np = Util::randNewPassword();
                if ($this->modules->User->resetPassword($_SESSION['findPasswordUserName'], $np)) {
                    $this->putIntoPage("newPassword", $np);
                    $_SESSION['findPasswordUserName'] = "";
                    $_SESSION['findPasswordEmail'] = "";
                } else
                {
                    $_SESSION['findPasswordUserName'] = "";
                    $_SESSION['findPasswordEmail'] = "";
                    $this->putIntoPage("error", "系统错误");
                }


            } else {
                $this->putIntoPage("error", "对不起，您输入的答案有误");
            }
        }

        $this->putIntoPage("step", $step);
        $this->render("front_password");
    }

}

?>